<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of pembelian
 *
 * @author fendi
 */
class pembelian extends My_Controller{
    var $data;
    function pembelian(){
        parent::My_Controller();
        $this->data['title']='Pembelian';
    }
    //menampilkan transaksi hari ini
    function index(){
        if(isset($_POST['tanggal'])){
            $tanggal=$_POST['tanggal'];
        }else{
            $tanggal='20'.Date('y-m-d');
        }

        $this->load->model('m_pembelian');
        $this->data['listPembelian']=$this->m_pembelian->getPembelianByDate($tanggal);
        $this->data['main_view']='pembelian/view_pembelian';
        $this->data['tanggal']=$tanggal;
        $this->render($this->data);
    }

    function transaksi_form(){
        $this->load->model('m_distributor');
        $this->data['main_view']='pembelian/transaksi_form';
        $this->data['subLink']='pembelian/transaksi_proses';
        $this->data['listDistributor']=$this->m_distributor->getAllDistributor();
        $this->render($this->data);
    }

    function transaksi_proses(){
        $this->load->model('m_pembelian');
        $table='';
        $detailPembelian=array();
        $id_pembelian=$this->input->post('id_pembelian');
        $this->m_pembelian->insertPembelian(array(
            'id_pembelian'=>$id_pembelian,
            'tgl_pembelian'=>Date('y-m-d'),
            'id_distributor'=>$_POST['id_distributor']));
        foreach($_POST['id_barang'] as $data=>$id_barang){
            $detailPembelian['id_pembelian']=$id_pembelian;
            $detailPembelian['id_barang']=$id_barang;
            $detailPembelian['jml_beli']=$_POST['jml'][$data];
            $detailPembelian['harga_beli']=$_POST['harga_beli'][$data];
            $this->m_pembelian->insertDetailPembelian($detailPembelian);
        }
        $this->index();
    }
}
?>
